/*
 *  Player.cpp
 *  RiskAI
 *
 */

#include "Player.h"

#include <sstream>

using namespace std;

//**********************************************************************************
//				CONSTRUCTION/DESTRUCTION
//**********************************************************************************

CPlayer::CPlayer(int iIndex, CBoard* pxBoard)
	: m_iIndex(iIndex)
	, m_pxBoard(pxBoard)
{
}

CPlayer::~CPlayer()
{
}




//**********************************************************************************
//				CORE FUNCTIONALITY
//**********************************************************************************

int
CPlayer::GetIndex()
{
	return m_iIndex;
}

//==================================================================================

vector<int>
CPlayer::GetTerritories()
{
	return m_viTerritories;
}

//==================================================================================

void
CPlayer::GainTerritory(int iTerritory)
{
	m_viTerritories.push_back(iTerritory);
}

//==================================================================================

void
CPlayer::LoseTerritory(int iTerritory)
{
	for(unsigned int i=0; i<m_viTerritories.size(); i++)
	{
		if(iTerritory == m_viTerritories[i])
		{
			m_viTerritories.erase(m_viTerritories.begin() + i);
			break;
		}
	}
}

//==================================================================================

bool
CPlayer::StillInGame()
{
	return m_viTerritories.size() > 0;
}

//==================================================================================

string
CPlayer::GetPlayerInfo()
{
	ostringstream o;
	o << "[Player " << m_iIndex << "] ";
	
	unsigned int iTerritories = m_viTerritories.size();
	
	if(iTerritories == 0)
	{
		o << "ELIMINATED";
	}
	else
	{
		int iNumTroops = 0;
		for(unsigned int i=0; i<iTerritories; i++)
			iNumTroops += m_pxBoard->GetTerritory(m_viTerritories[i])->GetNumArmies();
			
		o << "Armies: " << iNumTroops << "  Territories: " << iTerritories;
	}
	
	return o.str();
}




//**********************************************************************************
//				HELPER FUNCTIONS
//**********************************************************************************

int
CPlayer::StrengthOfWeaskestEnemy(int iTerritory)
{
	vector<int> viBorders = m_pxBoard->GetTerritory(iTerritory)->GetBorderIndices();
	
	int iWeakestForce = INFINITY;
	int iWeakestTarget = -1;
	
	for(unsigned int i=0; i<viBorders.size(); i++)
	{
		int iOwner = m_pxBoard->GetTerritory(viBorders[i])->GetOwner();
		if(iOwner != m_iIndex)
		{
			int iTroops = m_pxBoard->GetTerritory(viBorders[i])->GetNumArmies();
			if(iTroops < iWeakestForce)
			{
				iWeakestForce = iTroops;
				iWeakestTarget = viBorders[i];
			}
		}
	}
	
	return iWeakestForce;
}





//**********************************************************************************
//				VIRTUAL FUNCTIONS
//**********************************************************************************

int
CPlayer::PickTerritory(vector<int> viAvailables)
{
	return 0; // PLACEHOLDER
}

//==================================================================================

int
CPlayer::AssignTroops(int iNumTroops)
{
	return 0; // PLACEHOLDER
}

//==================================================================================

int
CPlayer::MoveTroops(int iSourceIndex, int iDestinationIndex)
{
	return 0; // PLACEHOLDER
}

//==================================================================================

RiskAction
CPlayer::DecideOnAction(vector<int> viViableAttackers)
{
	return END_TURN; // PLACEHOLDER
}

//==================================================================================

int
CPlayer::PickDefendingDie(int* aiAttackDice, int iTarget)
{
	return 0; // PLACEHOLDER
}

//==================================================================================

CFortification
CPlayer::FortifyTroops(vector<int> viFortifiers)
{
	return NO_FORTIFICATION; // PLACEHOLDER
}